package com.buercorp.wangyu.core1.ch12.threads;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ConditionExample {
    private Lock lock = new ReentrantLock();
    // 允许线程在某个条件下等待，直到另一个线程发出通知或满足某个条件时才继续执行
    private Condition condition = lock.newCondition();

    public void doSomething() {
        lock.lock();
        try {
            // 等待条件满足
            condition.await();

            // 条件满足后执行的操作
            System.out.println("条件满足，执行操作");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void changeCondition() {
        lock.lock();
        try {
            // 改变条件并通知其他等待的线程
            condition.signalAll();
        } finally {
            lock.unlock();
        }
    }


}
